<template>
  <div class='container'>
    <!-- 内容主体区域 -->
    <el-form
      ref="addFormRef"
      :model="form"
      label-width="80px"
      :rules="addFormRules"
    >
      <el-form-item label="所属学科">
        <el-select
          v-model="form.subjectID"
          placeholder="请选择"
          class="mm"
        >
          <el-option
            v-for="item in subjectLists"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>

        </el-select>
      </el-form-item>
      <el-form-item
        label="目录名称"
        prop="tagName"
      >
        <el-input
          class="mm"
          v-model="form.tagName"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>

    </el-form>
    <!-- 底部区域 -->
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="directorysAdd">取 消</el-button>
      <el-button
        type="primary"
        @click="addTags"
      >确 定</el-button>
    </span>
  </div>
</template>

<script>
import { add as tagsUpdate } from '@/api/hmmm/tags.js'
import { simple as subjectList } from '@/api/hmmm/subjects.js'
export default {
  name: 'directoryADD',
  props: {

  },
  data () {
    return {

      subjectLists: [],
      addDialogVisible: true,
      addFormRules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      },
      form: {
        subjectID: '',
        tagName: ''
      }

    }
  },
  created () {
    this.subjectList()
  },
  methods: {
    // 获取学科列表
    async subjectList () {
      try {
        const { data } = await subjectList()
        this.subjectLists = data
      } catch (error) {
        console.log(error)
      }
    },
    addTags () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        try {
          const { data } = await tagsUpdate(this.form)
          this.$message.success('添加数据成功')
          console.log(data)
          this.$emit('close')
        } catch (err) {
          this.$message.error('添加数据失败!')
        }
      })
    },
    // 控制添加目录对话框的显示
    directorysAdd () {
      this.addDialogVisible = false
      this.$emit('close')
    }
  }
}
</script>

<style scoped lang='scss'>
.dialog-footer {
  margin-left: 200px;
}
::v-deep .mm {
  width: 280px;
}
</style>
